// import store from '@/store'
import { APP_BASE_API } from "@/config";
// import debounce from "lodash/fp/debounce";
const PREFIX_TREND = `${APP_BASE_API}/quotation`;
// const PREFIX_CUSTOMER = APP_BASE_API + '/customer'
const PREFIX_INFORMATION = `${APP_BASE_API}/information`;

// const tologinPage = debounce(() => {
// 	// uni.showModal({
// 	//   title: '提示',
// 	//   content: '登录已过期，请重新登录',
// 	//   confirmText: '去登录',
// 	//   success: async res => {
// 	//     if (res.confirm) {
// 	//       wx.navigateTo({
// 	//         url: '/pages-account/login/login'
// 	//       })
// 	//     } else if (res.cancel) {
// 	//       console.log('用户点击取消重新登录')
// 	//     }
// 	//   }
// 	// })
// }, 200);
// there is no middle platform for low code plat

type resentry = {
	entity?: object;
};
const uploadTask = ({ url = "", tempFilePath = "", name = "file" } = {}) => {
	return new Promise((resolve, _reject) => {
		resolve("");
		// uni.File({
		//   url,
		//   filePath: tempFilePath,
		//   header: {
		//     // #ifdef MP-WEIXIN
		//     'content-type': 'multipart/form-data',
		//     // #endif
		//     'ZYL-USER-TOKEN': loadFromLocal('token') || '',
		//     'ZYL-BIZ-TYPE': 'ZYL-HQ-MP-OFFER', // 后端要求的  写死就可以
		//     // deviceId: store.state.deviceId,
		//     version: VERSION,
		//     marketCode: MARKET_CODE
		//   },
		//   name,
		//   formData: {},
		//   success: ({ data }) => {
		//     data = JSON.parse(data)
		//     if (data.ok) {
		//       return resolve(data)
		//     }
		//     // 401 或者 501 登入过期，重新登入
		//     if (data.code === '401' || data.code === '501') {
		//       store.dispatch('actionFedLogout')
		//       tologinPage()
		//     }
		//     reject(data)
		//   },
		//   fail: error => {
		//     console.log('上传失败', error)
		//     reject(error)
		//   }
		// })
	});
};

interface updateData {
	tempFilePath?: string;
	tempFilePaths?: string[];
}

interface UploadCallback {
	url: string;
	data: updateData;
	cbSuccess?: ()=> void;
	cbFail?: ()=> void;
	name?: string;
}
const uploadUtil = ({ url, data, name = "file" }: UploadCallback) => {
	return new Promise((resolve, reject) => {
		if (data.tempFilePath) {
			const tempFilePath = data.tempFilePath;
			uploadTask({ url, tempFilePath, name }).then(resolve).catch(reject);
		} else if (data.tempFilePaths) {
			const allPromises = data.tempFilePaths.map((tempFilePath) => {
				return uploadTask({ url, tempFilePath, name });
			});
			Promise.all(allPromises)
				.then((res) => {
					resolve(res.map((r: resentry) => r?.entity));
				})
				.catch(reject);
		}
	});
};

// 上传营业执照
export const apiUploadEntLicense = (tempFilePath = "") => {
	return uploadUtil({
		url: `${PREFIX_TREND}/v2/businessLicense/upload`,
		data: { tempFilePath },
	});
};

// 上传图片: 头像等
export const apiUploadImg = (tempFilePath = "") => {
	return uploadUtil({
		url: `${PREFIX_INFORMATION}/common/upload`,
		data: { tempFilePath },
	});
};

// 批量上传图片  含有内容安全检测
export const apiBatchSafeUpload = (tempFilePaths = []) => {
	return uploadUtil({
		url: `${PREFIX_INFORMATION}/common/csUpload`,
		data: { tempFilePaths },
	});
};

// 上传视频 含内容检测
export const apiSafeUploadVideo = (tempFilePath = "") => {
	return uploadUtil({
		url: `${PREFIX_INFORMATION}/video/upload`,
		data: { tempFilePath },
	});
};

// 上传语音
export const apiUploadAudio = ({ tempFilePath = "", duration = "" } = {}) => {
	return uploadUtil({
		url: PREFIX_INFORMATION + `/common/upload/audioFile?duration=${duration}`,
		// url: PREFIX_INFORMATION + '/common/upload/file',
		data: { tempFilePath },
		name: "multipartFile",
	});
};

// 上传文件
export const apiUploadFile = (tempFilePath = "") => {
	return uploadUtil({
		url: `${PREFIX_INFORMATION}/common/upload/file`,
		data: { tempFilePath },
	});
};
